﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OrionBank.Forms
{
    public partial class Compte : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        private Classe.Compte cA;
        private Classe.Utilisateur uA;
        public bool cont = false;

        public Compte(Int32 id)
        {
            InitializeComponent();
            kryptonHeader1.Values.Description = "Modification";
            try
            {
                cA = Classe.Compte.Charge(id);
                txtLibelle.Text = cA.Libelle;
                txtSoldeInitial.Value = new decimal(cA.SoldeInitial);
                kBanque.Text = cA.Banque;
                kGuichet.Text = cA.Guichet;
                kCompte.Text = cA.NoCompte;
                kClef.Text = cA.Clef;
                txtEvolSoldeMin.Value = cA.MinGraphSold;
                txtEvolSoldMax.Value = cA.MaxGraphSold;
                txtSeuilAlerte.Value = new decimal(cA.SeuilAlerte);
                cbEvolType.Text = cA.TypEvol;
                if(cA.TypEvol.Equals(string.Empty))
                {
                    kryptonRadioButton1.Checked = true;
                }
                else
                {
                    kryptonRadioButton2.Checked = true;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public Compte(Classe.Utilisateur u)
        {
            uA = u;
            InitializeComponent();
            kryptonHeader1.Values.Description = "Création";
            kryptonRadioButton1.Checked = true;
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if(ValideForm())
            {
                if(cA != null)
                {

                    try
                    {
                        cA.Libelle = txtLibelle.Text.Trim();
                        cA.SoldeInitial = Double.Parse(txtSoldeInitial.Value.ToString());
                        cA.Banque = kBanque.Text;
                        cA.Guichet = kGuichet.Text;
                        cA.NoCompte = kCompte.Text;
                        cA.Clef = kClef.Text;
                        cA.MinGraphSold = txtEvolSoldeMin.Value;
                        cA.MaxGraphSold = txtEvolSoldMax.Value;
                        cA.SeuilAlerte = Double.Parse(txtSeuilAlerte.Value.ToString());
                        if(kryptonRadioButton2.Checked)
                        {
                            cA.TypEvol = cbEvolType.Text;
                        }
                        else
                        {
                            cA.TypEvol = string.Empty;
                        }

                        Classe.Compte.Maj(cA);
                        cont = true;
                        this.Close();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if(uA != null)
                {
                    try
                    {
                        Classe.Compte c = new Classe.Compte();
                        c.Libelle = txtLibelle.Text.Trim();
                        c.SoldeInitial = Double.Parse(txtSoldeInitial.Value.ToString());
                        c.IdUtilisateur = uA.Id;
                        c.Banque = kBanque.Text;
                        c.Guichet = kGuichet.Text;
                        c.NoCompte = kCompte.Text;
                        c.Clef = kClef.Text;
                        c.MinGraphSold = txtEvolSoldeMin.Value;
                        c.MaxGraphSold = txtEvolSoldMax.Value;
                        if(kryptonRadioButton2.Checked)
                        {
                            c.TypEvol = cbEvolType.Text;
                        }
                        else
                        {
                            c.TypEvol = string.Empty;
                        }

                        Classe.Compte.Sauve(c);
                        cont = true;
                        this.Close();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private bool ValideForm()
        {
            bool retour = false;
            // Libelle du compte obligatoire
            if(txtLibelle.Text.Trim() != string.Empty)
            {
                // Controle de la clé RIB ?
                if(kBanque.Text.Trim() != string.Empty && kGuichet.Text.Trim() != string.Empty && kCompte.Text.Trim() != string.Empty)
                {
                    if(Outils.RIB.DonneCleRIB(kBanque.Text, kGuichet.Text, kCompte.Text).Equals(kClef.Text.Trim()))
                    {
                        retour = true;
                    }
                    else
                    {
                        MessageBox.Show("Attention clé RIB fournie (" + kClef.Text + ") non Valide.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    retour = true;
                }
            }
            else
                MessageBox.Show("Merci de remplir le champ Libelle.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);

            return retour;
        }

        private void kryptonRadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            checkEvol();
        }

        private void checkEvol()
        {
            txtEvolSoldeMin.Enabled = kryptonRadioButton1.Checked;
            txtEvolSoldMax.Enabled = kryptonRadioButton1.Checked;
            cbEvolType.Enabled = kryptonRadioButton2.Checked;
        }

        private void kryptonRadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            checkEvol();
        }
    }
}
